/*
 * Kinect Player
 * Kulikov Victor
 * kulikov.victor@gmail.com
 * GNU Lisence 2011
 */
#include "playcontrol.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QTime>
#define STR(x) QString("%1").arg(x)

PlayControl::PlayControl(QWidget *parent):
        QWidget(parent)
{
    init();
    setLength(100);
}

PlayControl::PlayControl(int length,QWidget *parent) :
    QWidget(parent)
{
    init();
    setLength(length);
}
void PlayControl::init()
{
    //Setting initial values for class members
    isHolded =false;
    m_rate = 25;
    filmpos=0;

    this->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Maximum);
    this->setFocusPolicy(Qt::NoFocus);

    //Create all items in the widget
    slider = new DubSlider();

    actionPlay = new QAction(QIcon(":/player/play"),tr("Play"),this);
    actionPause= new QAction(QIcon(":/player/pause"),tr("Pause"),this);
    actionStop= new QAction(QIcon(":/player/stop"),tr("Stop"),this);
    actionLeft= new QAction(QIcon(":/player/left"),tr("Left"),this);
    actionRight= new QAction(QIcon(":/player/right"),tr("Right"),this);
    actionClean= new QAction(QIcon(":/player/clean"),tr("Clean"),this);

    bplay = new QToolButton();
    bplay->setDefaultAction(actionPlay);
    bplay->setToolTip(tr("Play"));


    bpause = new QToolButton();
    bpause->setDefaultAction(actionPause);
    bpause->setToolTip(tr("Pause"));
    bpause->setEnabled(false);

    bstop = new QToolButton();
    bstop->setDefaultAction(actionStop);
    bstop->setToolTip(tr("Stop"));
    bstop->setEnabled(false);

    setLeft= new QToolButton();
    setLeft->setDefaultAction(actionLeft);
    setLeft->setToolTip(tr("Left"));
    setRight= new QToolButton();
    setRight->setDefaultAction(actionRight);
    setRight->setToolTip(tr("Right"));
    clean= new QToolButton();
    clean->setDefaultAction(actionClean);
    clean->setToolTip(tr("Clean"));

    fnumb = new QLabel(tr("Frames:  ")+ STR(0)+"\t["+QTime(0,0,0).toString("hh:mm:ss.zzz")+"]");
    fnumb->setFrameShape(QFrame::StyledPanel);
    fnumb->setFrameShadow(QFrame::Sunken);

    //Building layout
    QVBoxLayout *vlay = new QVBoxLayout();
    QHBoxLayout *hlay = new QHBoxLayout();

    vlay->addWidget(slider);
    hlay->addWidget(bplay);
    hlay->addWidget(bpause);
    hlay->addWidget(bstop);
    hlay->addWidget(setLeft);
    hlay->addWidget(setRight);
    hlay->addWidget(clean);
    hlay->addWidget(fnumb);
    vlay->addLayout(hlay);
    this->setLayout(vlay);

    //Signal connection
    connect(slider,SIGNAL(sliderReleased()),this,SLOT(valueChange()));
    connect(slider,SIGNAL(valueChanged(int)),this,SLOT(FrameChange(int)));
    connect(slider,SIGNAL(sliderPressed()),this,SLOT(holdSlider()));

    connect(bplay,SIGNAL(clicked()),this,SLOT(startResult()));
    connect(bpause,SIGNAL(clicked()),this,SIGNAL(sig_pause()));

    connect(setLeft,SIGNAL(clicked()),slider,SLOT(setLeftFlag()));
    connect(setRight,SIGNAL(clicked()),slider,SLOT(setRightFlag()));
    connect(clean,SIGNAL(clicked()),slider,SLOT(cleanFlags()));

    connect(bplay,SIGNAL(clicked()),this,SLOT(setPlayState()));
    connect(bpause,SIGNAL(clicked()),this,SLOT(setPlauseState()));


   // connect(bstop,SIGNAL(clicked()),this,SIGNAL(stop()));
    connect(bstop,SIGNAL(clicked()),this,SLOT(stopResult()));
    connect(bstop,SIGNAL(clicked()),this,SLOT(setStopState()));
}

void PlayControl::setRate(int rate)
{
    m_rate = rate;
}

void PlayControl::setPlayState()
{
    bplay->setDisabled(true);
    bpause->setEnabled(true);
    bstop->setEnabled(true);
}

void PlayControl::setPlauseState()
{
    bplay->setEnabled(true);
    bpause->setDisabled(true);
    bstop->setEnabled(true);
}

void PlayControl::setStopState()
{
    bplay->setEnabled(true);
    bpause->setDisabled(true);
    bstop->setDisabled(true);
}

void PlayControl::Moved(long pos)
{

    if(isHolded)
    {
        return;
    }
    //!!!!!!
    if(pos==slider->maximum()-1)//UNKNOWN BUG CRASHES IF POS == MAXIMUM
    {
        stopResult();
      //  emit stop();
        return;
    }

    if(-1!=slider->rightFlag())
    {
        if(pos==slider->rightFlag())
        {
            stopResult();
            return;
        }
    }
    filmpos = pos;
   // if(abs(pos-slider->value())<2)
  //      return;
    slider->setValue(pos);

}
void PlayControl::lockControl()
{
    bplay->setDisabled(true);
    bpause->setDisabled(true);
    bstop->setDisabled(true);
    setLeft->setDisabled(true);
    setRight->setDisabled(true);
    clean->setDisabled(true);
    if(-1!=slider->leftFlag())
    {
        emit seeked(slider->leftFlag());
        slider->setValue(slider->leftFlag());
    }
}

void PlayControl::unlockControl()
{
    setStopState();
    setLeft->setDisabled(false);
    setRight->setDisabled(false);
    clean->setDisabled(false);
}

void PlayControl::setLength(long lenght)
{
    slider->setRange(0,lenght);
}
void PlayControl::FrameChange(int pos)
{
    filmpos = pos;
    QTime t = QTime(0,0,0).addMSecs((1000/m_rate)*pos);
    fnumb->setText(tr("Frames:  ")+STR(pos)+"\t["+t.toString("hh:mm:ss.zzz")+"]");
}
void PlayControl::next()
{
    filmpos=filmpos+1;
    Moved(filmpos);
}

void PlayControl::valueChange()
{
    emit seeked(slider->value());
    isHolded = false;
}
void PlayControl::holdSlider()
{
    isHolded = true;
}
void PlayControl::stopResult()
{
    emit stop();
    int newpos=0;
    if(-1!=slider->leftFlag())
    {
        newpos=slider->leftFlag();
    }
    filmpos=0;
    setStopState();
    emit seeked(0);
    slider->setValue(newpos);
}
void PlayControl::startResult()
{
    if(-1!=slider->leftFlag())
    {
        emit seeked(slider->leftFlag());
        slider->setValue(slider->leftFlag());
    }
    emit sig_play();
}
